package edu.vt.ece4564.bluetendance.server;

import java.net.UnknownHostException;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class ServerLauncher {
	
	private Server mServer; 
	
	public boolean start() {		
		try {
			
			WebAppContext context = new WebAppContext();
			context.setDescriptor("war/WEB-INF/web.xml");
			context.setResourceBase("war");
			context.setContextPath("/");
			
			System.setProperty("org.eclipse.jetty.LEVEL", "WARN");
			mServer = new Server(8080);
			mServer.setHandler(context);
			mServer.start();
			System.out.println("[Server started]\n");
			
		} catch (UnknownHostException e) {
			System.out.println("[Error] Unable to start web server");
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			System.out.println("[Error] Unable to start web server");
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public void stop(int delay) {
		try {
			mServer.stop();
			System.out.println("[Server halted]");
		} catch (Exception e) {
			System.out.println("[Error] Unable to stop the server");
		}
	}

	public static void main(String[] args) {
		ServerLauncher server = new ServerLauncher();

		if (!server.start())
			return;
	}
}
